﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BesAsm.Framework.CommonControls;
using BesAsm.MasterData.Gis;
using BesAsm.MasterData.Services;
using BesAsm.MasterData.Entities;

namespace BesAsm.MasterData.CommonControls
{
  /// <summary>
  /// A user-input dialog which will return Link entities from a TList&ltLink&gt collection based on
  /// a user's input. Allows for multiple selection method, including LinkName, Upstream Node Name and
  /// Downstream Node Name
  /// </summary>
  public partial class LinkInputDialog : TextInputDialog
  {
    private TList<Link> _links;

    /// <summary>
    /// Constructs a new LinkInputDialog using the provided TList
    /// </summary>
    /// <param name="links">The TList to select from</param>
    public LinkInputDialog(TList<Link> links)
      : base()
    {
      InitializeComponent();
      DropDownItems = new string[] { LinkInfo.LinkCaption, LinkInfo.UsNodeCaption, LinkInfo.DsNodeCaption };
      DropDownLabelText = "Select using:";
      UseDownDown = true;
      cmbItems.SelectedIndex = DropDownItemDefaultIndex;
      LastEntryItemsVisible = true;
      _links = links;
    }

    /// <summary>
    /// Gets the Oids input by the user after a call to ShowDialog
    /// </summary>
    /// <returns></returns>
    public int[] Oids()
    {
      TList<Link> links = Links();
      return links.Select(p => p.Oid).ToArray();
    }

    /// <summary>
    /// Gets the Links collection input by the user after a call to ShowDialog()
    /// </summary>
    /// <returns>The Links collection selected by the user</returns>
    public TList<Link> Links()
    {
      string selectionType = this.SelectedDropDownItem;

      string[] items = this.InputText.Split(new char[] { '\n', '\r', '=' }, StringSplitOptions.RemoveEmptyEntries);

      TList<Link> links = null;
      if (selectionType == LinkInfo.LinkCaption)
        links = GetByLinkName(items);
      else if (selectionType == LinkInfo.UsNodeCaption)
        links = GetByUsNodeName(items);
      else if (selectionType == LinkInfo.DsNodeCaption)
        links = GetByDsNodeName(items);

      return links;
    }

    private TList<Link> GetByDsNodeName(string[] items)
    {
      NodeService ns = new NodeService();
      BesAsm.MasterData.Data.Bases.NodeParameterBuilder npb = new BesAsm.MasterData.Data.Bases.NodeParameterBuilder();
      npb.AppendIn(NodeColumn.NodeName, items);
      TList<Node> nodes = ns.Find(npb.GetParameters());

      List<Link> list = _links
        .Where(p => nodes
          .Select(node => node.NodeId)
          .Contains(p.DsNodeId))
        .ToList();

      return new TList<Link>(list);
    }

    private TList<Link> GetByUsNodeName(string[] items)
    {
      NodeService ns = new NodeService();
      BesAsm.MasterData.Data.Bases.NodeParameterBuilder npb = new BesAsm.MasterData.Data.Bases.NodeParameterBuilder();
      npb.AppendIn(NodeColumn.NodeName, items);
      TList<Node> nodes = ns.Find(npb.GetParameters());

      List<Link> list = _links
        .Where(p => nodes
          .Select(q => q.NodeId)
          .Contains(p.UsNodeId))
        .ToList();

      return new TList<Link>(list);
    }

    private TList<Link> GetByLinkName(string[] items)
    {
      try
      {
        List<Link> list = _links
          .Where(p => items.Contains(p.LinkName)).ToList();

        TList<Link> links = new TList<Link>(list);
        return links;
      }
      catch (Exception ex)
      {
        return null;
      }

    }

    private void cmbLastEntry_ValueChanged(object sender, EventArgs e)
    {
      string[] items = cmbLastEntry.Text.Split(',');
      txtInput.Clear();
      foreach (string item in items)
      {
        txtInput.AppendText(item + Environment.NewLine);
      }
    }

    public string[] LineItems()
    {
      List<string> lines = new List<string>();
      using (System.IO.StringReader reader = new System.IO.StringReader(txtInput.Text))
      {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
          lines.Add(line);
        }
      }
      return lines.ToArray();
    }
  }

}
